package com.iris.live.services.report.data.baseline;

import com.iris.live.services.data.models.ReportConfig;
import com.iris.live.services.report.data.ReportDataConvertor;
import com.iris.live.services.report.data.formulars.FormularFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:按照基础轴转换数据
 * @author: Tony.Zhang
 * @Date: 2017-08-25 13:55
 **/
@Component("BaselineConvertor")
public class BaselineConvertor  extends ReportDataConvertor<BaselineContext> {
    public BaselineConvertor() {
    }

    @Override
    protected void getData(BaselineContext context, Map reportRow, Map resultSet, ReportConfig config) throws Exception {
        Map resultCell = null;
        String configCode = config.getValueKey(), fullPath = this.getFullPath(config);
        Object value = null;

        List<BaseUnit> units = context.getUnits();
        if(config.isBlank()) {
            for (BaseUnit unit : units) {
                resultCell = (Map) reportRow.get(fullPath + "_" + unit.getValue());
                if (resultCell == null) {
                    resultCell = new HashMap<>();
                    resultCell.put("color", 0);
                    resultCell.put("displayRow", unit.getValue());
                    resultCell.put("displayRowOrder", unit.getDisplayRowOrder());
                    resultCell.put(config.getCategory() + "Value", null);
                }
                reportRow.put(fullPath + "_" + unit.getValue(), resultCell);
            }
        }

        if(resultSet.get(configCode) != null){
            value = resultSet.get(configCode);
        }else{
            value = 0;
        }

        boolean available = false;
        for(BaseUnit unit : units){
            resultCell = (Map)reportRow.get(fullPath+"_"+unit.getValue());
            if(resultCell == null){
                resultCell = new HashMap<>();
                resultCell.put("color", 0);
                resultCell.put("displayRow", unit.getValue());
                resultCell.put("displayRowOrder", unit.getDisplayRowOrder());

                this.setResultCell(resultCell, config, 0);
            }

            //判断当前记录是否满足Y轴统计条件
            available = unit.getType().equals(BaseUnit.BaseType.TOTAL);
            if(available){
                this.setResultCell(resultCell, config, value);
            }

            String day = resultSet.get(unit.getKey()).toString();
            available = unit.getValue().equals(day);
            if(available){
                this.setResultCell(resultCell, config, value);
            }

            reportRow.put( fullPath+"_"+unit.getValue(), resultCell);
        }
    }

    @Override
    protected void getFormularData(BaselineContext context, Map<String, Map> datas, Map reportRow, ReportConfig config) throws Exception {
        String fullPath = this.getFullPath(config), formularExpression = config.getFormularExpression();

        String formular = formularExpression.substring(0, formularExpression.indexOf("("));
        Map resultCell = null;

        List<BaseUnit> units = context.getUnits();
        for(BaseUnit unit : units){ //按照用户设置的基础轴上的数据和当前数据进行比较并进行合计
            resultCell = (Map) reportRow.get(fullPath + "_" + unit.getValue());
            if (resultCell == null) {
                resultCell = new HashMap<>();
                resultCell.put("color", 0);
                resultCell.put("displayRow", unit.getValue());
                resultCell.put("displayRowOrder", unit.getDisplayRowOrder());
                resultCell.put(config.getCategory() + "Value", 0);
            }

            Object value = FormularFactory.getFormular(formular).getValue(config, unit.getValue(), datas);
            this.setResultCell(resultCell, config, value);
            reportRow.put(fullPath + "_" + unit.getValue(), resultCell);
        }
    }
}
